/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.slayer.service.impl;

import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portlet.shopping.model.ShoppingItem;
import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
import com.slayer.NoSuchShoppingCartExtException;
import com.slayer.model.CartItem;
import com.slayer.model.ShoppingCartExt;
import com.slayer.service.base.ShoppingCartExtLocalServiceBaseImpl;

/**
 * The implementation of the shopping cart ext local service.
 * 
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link com.slayer.service.ShoppingCartExtLocalService} interface.
 * 
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 * 
 * @author ahmedhasan
 * @see com.slayer.service.base.ShoppingCartExtLocalServiceBaseImpl
 * @see com.slayer.service.ShoppingCartExtLocalServiceUtil
 */
public class ShoppingCartExtLocalServiceImpl extends
		ShoppingCartExtLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 * 
	 * Never reference this interface directly. Always use {@link
	 * com.slayer.service.ShoppingCartExtLocalServiceUtil} to access the
	 * shopping cart ext local service.
	 */

	public ShoppingCartExt fetchByCartId_ItemId(long cartId, long itemId)
			throws SystemException {
		return shoppingCartExtPersistence.fetchByCartId_ItemId(cartId, itemId);
	}

	public List<ShoppingCartExt> getByCartId(long cartId)
			throws SystemException {
		return shoppingCartExtPersistence.findByCartId(cartId);
	}

	public void persistCartExt(long itemId, long cartId, CartItem item) {
		// TODO Auto-generated method stub

		ShoppingItem shoppingItem = null;
		boolean variableAmount = false;
		try {
			shoppingItem = ShoppingItemLocalServiceUtil.fetchShoppingItem(itemId);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (Validator.isNull(shoppingItem)) {
			return;
		} else {
			variableAmount = (shoppingItem.getPrice() == 0.0);
		}
		
		ShoppingCartExt shoppingCartExt = null;

		try {
			shoppingCartExt = fetchByCartId_ItemId(cartId, itemId);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		if (Validator.isNull(shoppingCartExt)) {
			long recId = 0l;
			try {
				recId = CounterLocalServiceUtil.increment();
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			shoppingCartExt = createShoppingCartExt(recId);
			try {
				shoppingCartExt = addShoppingCartExt(shoppingCartExt);
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			shoppingCartExt.setAmount((int) item.getAmount());
			shoppingCartExt.setQty(item.getQty());
		} else {
			shoppingCartExt.setAmount((int) item.getAmount() + (int)shoppingCartExt.getAmount());
			
			int oldQty = variableAmount? 0 : shoppingCartExt.getQty();
			shoppingCartExt.setQty(item.getQty() + oldQty);
		}

		shoppingCartExt.setFrequency(item.getFrequency());
		shoppingCartExt.setItemId(itemId);
		shoppingCartExt.setCartId(cartId);

		try {
			updateShoppingCartExt(shoppingCartExt);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void removeItemFromCart(long cartId, long itemId) {
		try {
			shoppingCartExtPersistence.removeByCartId_ItemId(cartId, itemId);
		} catch (NoSuchShoppingCartExtException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void removeCart(long cartId) {
		try {
			shoppingCartExtPersistence.removeByCartId(cartId);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}